
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<link rel="stylesheet" type="text/css" href="../../css/bootstrap.css">
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ZCXPWxfUzpReuZYnZWXeXbvq725yGo1I"></script>
	<!--加载鼠标绘制工具-->
	<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
	<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
	<!--加载检索信息窗口-->
	<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
	<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />
	<script type="text/javascript" src="../../js/jquery.min.js"></script>
	<title>查看电子围栏</title>
	<style type="text/css">
		body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
		#allmap {width: 100%; height:500px; overflow: hidden;}
		#result {width:100%;font-size:12px;}
		dl,dt,dd,ul,li{
			margin:0;
			padding:0;
			list-style:none;
		}
		p{font-size:12px;}
		dt{
			font-size:14px;
			font-family:"微软雅黑";
			font-weight:bold;
			border-bottom:1px dotted #000;
			padding:5px 0 5px 5px;
			margin:5px 0;
		}
		dd{
			padding:5px 0 0 5px;
		}
		li{
			line-height:28px;
		}
	</style>
</head>
<body>
	<div id="allmap" style="overflow:hidden;zoom:1;position:relative;">	
		<div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;"></div>
	</div>
	<div style="padding-top:10px; text-align: center;">
		<input type="hidden" id="xys"/>
		<button onclick="clearAll();" class="btn btn-danger">修改电子围栏</button>
		<button onclick="updOverlayXy();" class="btn btn-danger">确认修改</button>
	</div>
</body>
<script type="text/javascript">
	var href = location.href;
	href = href.split("=");
	href = href[1].split(";");
	
	// 百度地图API功能
    var map = new BMap.Map("map");
	var point = new BMap.Point(href[0].split(",")[0],href[0].split(",")[1]);
	map.centerAndZoom(point, 17);	// 初始化地图,用坐标点设置地图中心点
	map.enableScrollWheelZoom(true);	//允许缩放地图
	
	var pointArray = new Array(href.length-1);
	
	for(var i = 0; i < href.length-1; i++){
		var xy = href[i].split(",");
		pointArray[i] = new BMap.Point(xy[0],xy[1]);
		
		if(i == href.length-2){
			var polygon = new BMap.Polygon(pointArray, {strokeColor:"red", strokeWeight:3, strokeOpacity:0.5});  //创建多边形 覆盖物
		  	//添加覆盖物
			map.addOverlay(polygon);  //增加多边形覆盖物(电子围栏)
		}
	}
	
	var overlays = [];
	var overlaycomplete = function(e){
        overlays.push(e.overlay);
        getXYs(overlays);
    };
  	//将划出来的电子围栏坐标保存起来
    function getXYs(polygon){
  		
   		if(confirm("确定保存该电子围栏吗?")){
   			var polygonLen = polygon.length;
   			if(polygonLen > 1){
   				alert("禁止一次设置多个电子围栏!");
   	   			clearAll();
   			}else{
			    for(var i = 0; i < polygonLen; i++){
			       	var dd = polygon[i].po;
		   			if(dd.length < 3){
		   				alert("该电子围栏范围无效,请重新设置!");
		   	   			clearAll();
		   			}else{
			        	for(var j = 0; j < dd.length; j++){
			        		var lng = dd[j].lng;
			        		var lat = dd[j].lat;
			        		$("#xys").val($("#xys").val() + lng + "," + lat + ";");
			        	}
			    	}
	   			}
		    }
   		}else{
   			clearAll();
   		}
    }
    var styleOptions = {
       strokeColor:"red",    //边线颜色。
       fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
       strokeWeight: 3,       //边线的宽度，以像素为单位。
       strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
       fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
       strokeStyle: 'solid' //边线的样式，solid或dashed。
    }
       
    //实例化鼠标绘制工具
    var drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true, //是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(5, 5), //偏离值
    		drawingModes:[
 		 		BMAP_DRAWING_POLYGON	
 		 	]
        },
       	circleOptions: styleOptions, //圆的样式
        polylineOptions: styleOptions, //线的样式
        polygonOptions: styleOptions, //多边形的样式
        rectangleOptions: styleOptions //矩形的样式
    });
    
	 //添加鼠标绘制工具监听事件，用于获取绘制结果 
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);

	//确认修改电子围栏
    function updOverlayXy(){
    	if(overlays.length == 0){
    		alert("未设置电子围栏!");
    	}else{
        	var xy = $("#xys").val();
        	var id = window.parent.$("#id").val();
        	$.ajax({
				url : "../../region/updatePolygonLen.ao",
				type : 'POST',
				data : {
					"id" : id,
					"xy" : xy
				},
				cache : false,
				dataType : "json",
				success : function(data) {
					if (data["state"] == "success") {
						alert("电子围栏修改成功！");
						window.parent.$("#setPolygonInfoDiv").dialog("close");
					} else {
						alert(data["result"]);
					}
				}
			}); 
    	}
    }
    function clearAll(){
    	map.clearOverlays();
    }
</script>
</html>
